package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public List<Address> findAllPoint() {
        return addressDao.findAllPoint();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //分页助手
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Address> page = addressDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        //先解除地址与订单的关联
        long count = addressDao.findCountByAddressId(id);
        if (count != 0){
            addressDao.delRelOrederAndress(id);
        }
        addressDao.deleteById(id);
    }

    @Override
    public void add(Address address) {
        addressDao.add(address);
    }

    @Override
    public Address findById(Integer id) {
        //查询套餐表，将基本信息查询出来
        //根据套餐id查询关联的检查组，再将查询出的检查组集合赋值给套餐对象
        //根据检查组id查询关联的检查项集合，将集合赋值给检查组对象
        return addressDao.findById(id);
    }

    @Override
    public void edit(Address address) {
            addressDao.edit(address);
    }


}
